package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;


import controller.ApplicationEventListener;



public class ApplicationWindow extends JFrame {
	
	
	private static final long serialVersionUID = 1L;
	private JToolBar toolBarStack = new JToolBar();
	private DrawWindow drawWindow;
	private ApplicationEventListener appListener; 
	
	
	public ApplicationWindow(){
		
		setTitle("Stack & Queue Visualiser");
		setResizable(false);
		
		drawWindow = new DrawWindow();
		//respond to window asking to close.
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		appListener = new ApplicationEventListener(toolBarStack, drawWindow);
			
		MenuBar mb = new MenuBar();
		mb = addMenuBar(mb);
		setMenuBar(mb); // Frame implements MenuContainer
		
		JToolBar toolBar = new JToolBar();
		addToolBarButtons(toolBar);

		// Put it in a scrollPane, (this makes a border)
		JScrollPane scrollPane = new JScrollPane(drawWindow);

		// Lay out the content pane.
		JPanel contentPane = new JPanel();
		contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		contentPane.setLayout(new BorderLayout());
		contentPane.setPreferredSize(new Dimension(1200, 800));
		contentPane.add(toolBar, BorderLayout.WEST);
		contentPane.add(scrollPane, BorderLayout.CENTER);
				
		setContentPane(contentPane);		
				
		pack();
		setLocationRelativeTo(null);
		this.setVisible(true);
				
	}
	
	private void addToolBarButtons(JToolBar toolBar) {

		addStackButtons(toolBarStack);
		toolBar.add(toolBarStack);

		toolBar.setFloatable(false);
		toolBar.setOrientation(JToolBar.VERTICAL);
		
	}
	
	//Defaults Button Size
	Insets btnSize = new Insets(10,10,10,10);
	
	protected void addStackButtons(JToolBar toolBar) {


		toolBar.setFloatable(false);
		toolBar.setOrientation(JToolBar.VERTICAL);
		ButtonGroup stackButtons = new ButtonGroup();

		//Create Play Button
		ImageIcon add = new ImageIcon ("ADD.png");
		JButton pushBtn = new JButton(add);
		pushBtn.setToolTipText("Add an element to the stack");
		pushBtn.setMargin(btnSize);
		pushBtn.addActionListener(appListener);
		pushBtn.setActionCommand("PUSH");
		toolBar.add(pushBtn);	
		stackButtons.add(pushBtn);

		//Create Pause Button
		ImageIcon remove = new ImageIcon ("REMOVE.png");
		JButton popBtn = new JButton(remove);
		popBtn.setToolTipText("Remove an element from the stack");
		popBtn.setMargin(btnSize);
		popBtn.addActionListener(appListener);
		popBtn.setActionCommand("POP");
		toolBar.add(popBtn);
		stackButtons.add(popBtn);
		
		ImageIcon addPredict = new ImageIcon ("ADDPREDICT.png");
		JButton pushPredictBtn = new JButton(addPredict);
		pushPredictBtn.setToolTipText("Predict what element will be added to the stack");
		pushPredictBtn.setMargin(btnSize);
		pushPredictBtn.addActionListener(appListener);
		pushPredictBtn.setActionCommand("ADDPREDICT");
		toolBar.add(pushPredictBtn);
		stackButtons.add(pushPredictBtn);
		
		ImageIcon predictRemove = new ImageIcon ("REMOVEPREDICT.png");
		JButton popPredictBtn = new JButton(predictRemove);
		popPredictBtn.setToolTipText("Predict what element will be removed from the stack");
		popPredictBtn.setMargin(btnSize);
		popPredictBtn.addActionListener(appListener);
		popPredictBtn.setActionCommand("REMOVEPREDICT");
		toolBar.add(popPredictBtn);
		stackButtons.add(popPredictBtn);
	
	}
	
	private MenuBar addMenuBar(MenuBar mb){
		
		Menu fm, im, hm;
		MenuItem mi;
		
		// The File Menu
		fm = new Menu("File");
		fm.add(mi = new MenuItem("New Stack...", new MenuShortcut(KeyEvent.VK_S, false)));
		mi.setActionCommand("NEW_STACK");
		mi.addActionListener(appListener); 
		fm.add(mi = new MenuItem("New Queue...", new MenuShortcut(KeyEvent.VK_Q, false)));
		mi.setActionCommand("NEW_QUEUE");
		mi.addActionListener(appListener);
		
		fm.addSeparator();
		
		fm.add(mi = new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_E, false)));
		mi.setActionCommand("EXIT");
		mi.addActionListener(appListener);
		mb.add(fm);

		// The Mode Menu
		im = new Menu("Implementations");
		im.add(mi = new MenuItem("Array Stack", new MenuShortcut(KeyEvent.VK_M, false)));
		mi.setActionCommand("ARRAYSTACK");
		mi.addActionListener(appListener);
		
		fm.addSeparator();	
	
		im.add(mi = new MenuItem("Array Queue", new MenuShortcut(KeyEvent.VK_Y, false)));
		mi.setActionCommand("ARRAYQUEUE");
		mi.addActionListener(appListener);
		
		mb.add(im);

		// The Help Menu
		hm = new Menu("Help");

		hm.add(mi = new MenuItem("About", new MenuShortcut(KeyEvent.VK_A, false)));
		mi.setActionCommand("ABOUT");
		mi.addActionListener(appListener);
		//mi.addActionListener(appListener);
		mb.add(hm);

		return mb;

	}

}
